package com.guet.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guet.DTO.VideoDTO;
import com.guet.DTO.VideoPageDTO;
import com.guet.Entity.Video;
import com.guet.VO.VideoVO;
import com.guet.context.BaseContext;
import com.guet.mapper.VideoMapper;
import com.guet.result.PageResult;
import com.guet.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;

    /**
     * 添加视频
     *
     * @param videoDTO
     */
    @Override
    public void addVideo(VideoDTO videoDTO) {
        Long userId = BaseContext.getCurrentId();
        Video video = new Video();
        BeanUtils.copyProperties(videoDTO, video);
        video.setUserId(userId);
        video.setCreateTime(LocalDateTime.now());
        videoMapper.insert(video);
    }

    /**
     * 分页查询视频列表
     *
     * @param videoPageDTO
     * @return
     */
    @Override
    public PageResult list(VideoPageDTO videoPageDTO) {
        PageHelper.startPage(videoPageDTO.getCurrentPage(), videoPageDTO.getPageSize());
        Page<VideoVO> result = videoMapper.list(videoPageDTO);
        Long total = result.getTotal();
        List<VideoVO> list = result.getResult();
        return new PageResult(total, list);
    }

    /**
     * 根据id查询视频
     * @param id
     * @return
     */
    @Override
    public VideoVO queryById(Integer id) {
        return videoMapper.queryById(id);
    }

    /**
     * 根据id删除视频
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        videoMapper.deleteById(id);
    }
}
